<template>
  <div>
    <body id="paper">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="login-container"
      label-position="left" label-width="0px"
      >
      <h3 class="login_title">注册</h3>
      <el-form-item prop="tusername">
      <el-input type="text" v-model="ruleForm.tusername"
                auto-complete="off" placeholder="账号"></el-input>
    </el-form-item>
        <el-form-item prop="tpassword">
      <el-input type="password" v-model="ruleForm.tpassword"
                auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
        <el-form-item prop="checkPass">
      <el-input type="password" v-model="ruleForm.checkPass"
                auto-complete="off" placeholder="确认密码"></el-input>
    </el-form-item>
        <el-form-item prop="tname">
      <el-input type="text" v-model="ruleForm.tname"
                auto-complete="off" placeholder="真实姓名"></el-input>
    </el-form-item>
        <el-form-item prop="ttel">
      <el-input type="text" v-model.number="ruleForm.ttel"
                auto-complete="off" placeholder="手机号"></el-input>
    </el-form-item>
        <el-form-item prop="temail">
      <el-input type="text" v-model="ruleForm.temail"
                auto-complete="off" placeholder="邮箱"></el-input>
    </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%;background: #1890ff;border: none;" @click="submitForm('ruleForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </body>
  </div>
</template>
<script>
export default {
  components: {
  },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.tpassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        tusername:"",
        tpassword: "",
        checkPass: "",
        tname: "",
        ttel:"",
        temail:""
      },
      rules: {
        tpassword: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$store.dispatch("userStore/fetchReg",this.ruleForm);
          alert("注册成功!");
          this.$router.push({name:"Menu"})

        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style>
  #paper {
    background-position: center;
    height: 100%;
    width: 100%;
    background-size: cover;
    position: fixed;
  }
  body{
    margin: -5px 0px;
  }
  .login-container {
    border-radius: 15px;
    background-clip: padding-box;
    margin: 160px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }
  .login_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .login_remember {
    margin: 0px 0px 35px 0px;
    text-align: left;
  }
</style>